package com.chengyu.core.controller;

import com.chengyu.core.MyWordReplace;
import com.chengyu.core.entity.CommonPage;
import com.chengyu.core.entity.CommonResult;
import com.chengyu.core.model.Assist;
import com.github.houbb.sensitive.word.bs.SensitiveWordBs;
import com.github.houbb.sensitive.word.core.SensitiveWordHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.regex.Matcher;
import java.util.regex.Pattern;


@Api(tags = "敏感词过滤", description = "SensitiveWordController")
@Controller
@RequestMapping("/sensitiveword")
public class SensitiveWordController extends UserBaseController {
    @Autowired
    private MyWordReplace myWordReplace;
    @Autowired
    private SensitiveWordBs sensitiveWordBs;
    private static final String text = "五星红旗迎风飘扬，毛主席的画像屹立在天安门前,zhuzhuhzu";
    private static final String textDenyTest = "裸聊";

//
//    public static String filterSensitiveWords(String text, String sensitiveWord) {
//        Pattern pattern = Pattern.compile(sensitiveWord);
//        Matcher matcher = pattern.matcher(text);
//        return matcher.replaceAll("***");
//    }

//    @GetMapping("/pattern")
//    public void testSensitiveWord2() {
//        String text = "这是一个包含敏感词汇的文本，例如色情、赌博等。";
//        String[] sensitiveWords = {"色情", "赌博"};
//        for (String word : sensitiveWords) {
//            text = filterSensitiveWords(text, word);
//        }
//        System.out.println("过滤后的文本： " + text);
//    }

    /**
     * 方案二：基于DFA算法的敏感词过滤工具框架-sensitive-word:https://github.com/houbb/sensitive-word
     * 6W+ 词库，且不断优化更新
     * 基于 DFA 算法，性能较好
     * 基于 fluent-api 实现，使用优雅简洁
     * 支持敏感词的判断、返回、脱敏等常见操作
     * 支持全角半角互换
     * 支持英文大小写互换
     * 支持数字常见形式的互换
     * 支持中文繁简体互换
     * 支持英文常见形式的互换
     * 支持用户自定义敏感词和白名单
     * 支持数据的数据动态更新，实时生效
     */
    @ApiOperation(value = "过滤")
    @RequestMapping(value = "/filter", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<String> testSensitiveWord() {
        System.out.println("SensitiveWordHelper.contains(text) = " + SensitiveWordHelper.contains(text));
        System.out.println("SensitiveWordHelper.findAll(text) = " + SensitiveWordHelper.findAll(text));
        System.out.println("SensitiveWordHelper.replace(text,myWordReplace) = " + SensitiveWordHelper.replace(text, myWordReplace));

        // 如果自定义敏感词，不要使用SensitiveWordHelper的方法，要使用SensitiveWordBs
        System.out.println("sensitiveWordBs.contains(text) = " + sensitiveWordBs.contains(textDenyTest));
        System.out.println("sensitiveWordBs.findAll(text) = " + sensitiveWordBs.findAll(textDenyTest));
        System.out.println("sensitiveWordBs.replace(text) = " + sensitiveWordBs.replace(textDenyTest));
        return CommonResult.success(null);
    }

//    private final SensitiveWordBs sensitiveWordBs;
//    private final WordDenyService wordDenyService;
//    private final WordAllowService wordAllowService;
//
//    @PostMapping
//    public Res word(@RequestBody WordVo wordVo) {
//
//        String res = "";
//        String allowRes = "数据库中白名单为空";
//        WordDeny wordDeny = wordDenyService.getOne(Wrappers.emptyWrapper());//配置
//        List<WordAllow> wordAllowList = wordAllowService.list(Wrappers.emptyWrapper());//白名单
//        if (wordAllowList != null && wordAllowList.size() > 0) {
//
//            allowRes = wordAllowList.toString();
//        }
//        if (wordDeny == null) {
//
//            res = "数据库没有配置敏感词，使用默认的敏感词和白名单";
//        } else {
//
//            res = "数据库【" + (wordDeny.getDefaultDeny() ? "开启默认敏感词" : "【开启自定义敏感词】,自定义值:" + wordDeny.getWordDeny()) + "】，"
//                    + (wordDeny.getDefaultAllow() ? "【开启默认白名单】" : "【开启自定义白名单】,自定义值:" + allowRes);
//        }
//        String word = wordVo.getWord();
//        System.out.println("\33[36;1m该请求的设置：\033[0m" + res);
//        System.out.println("\33[36;1m请求中被检测的字符串：\033[0m" + word);
//        System.out.println("\33[36;1m字符串中是否包含铭感词：\033[0m" + (sensitiveWordBs.contains(word) ? "【包含】" : "【不包含】"));
//        System.out.println("\33[36;1m查找所有铭感词：\033[0m" + sensitiveWordBs.findAll(word));
//        System.out.println("\33[36;1m查找第一个铭感词：\033[0m" + sensitiveWordBs.findFirst(word));
//        System.out.println("\33[36;1m替换所有铭感词(默认*)：\033[0m" + sensitiveWordBs.replace(word));
//        List<IWordResult> wordList3 = sensitiveWordBs.findAll(word, WordResultHandlers.raw());
//        System.out.println("\33[36;1m查找敏感词单词本身的起始位置到终止位置：\033[0m" + wordList3);
//        return Res.ok(res);
//    }

}

